package com.qhit.controller;

import com.qhit.pojo.Role;
import com.qhit.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @RequestMapping("/list")
    public String list(Model m,String keywords,
                       @RequestParam(defaultValue = "1",required = false)
                       Integer page,
                       @RequestParam(defaultValue = "1",required = false)
                       Integer pageSize){
        HashMap hashMap = new HashMap<>();
        hashMap.put("keywords",keywords);
        hashMap.put("start",(page-1)*pageSize);
        hashMap.put("pageSize",pageSize);
        hashMap.put("page",page);
        List<Role> roleList=roleService.getList(hashMap);
        int total= roleService.getListCount(hashMap);
        hashMap.put("totalPage",(total%pageSize==0)?(total/pageSize):(total/pageSize+1));
        m.addAttribute("roleList",roleList);
        m.addAttribute("myParams",hashMap);
        return "role/list";
    }
    @RequestMapping("toAdd")
    public String toAdd(){
        return "role/add";
    }
    @RequestMapping(value = "/add",method =RequestMethod.POST)
    public String add(Role role, Model m,String doubleTime){

       int r= this.roleService.add(role);
        if (r>0){
            m.addAttribute("msg","添加成功！！");
        }else {
            m.addAttribute("msg","添加失败！！");
        }
        return "role/msg";
    }
    @RequestMapping("/toEdit")
    public String toEdit(int id,Model model){
        Role g=roleService.getRoleById(id);
        model.addAttribute("role",g);
        return "role/edit";
    }
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String edit(Role g,Model model,String doubleTime){

        int re=roleService.edit(g);
        if (re>0){
            model.addAttribute("msg","修改成功！！");
        }else {
            model.addAttribute("msg","修改失败！！");
        }
        return "role/msg";
    }
    @RequestMapping(value = "/del",method = RequestMethod.GET)
    public String del(int id,Model model){

        int re=roleService.del(id);
        if (re>0){
            model.addAttribute("msg","删除成功！！");
        }else {
            model.addAttribute("msg","删除失败！！");
        }
        return "role/msg";
    }

}
